package lesson02;

import java.util.concurrent.TimeUnit;

public class VolatileDemo {

    volatile Integer runing = 1;

    void m() {
        System.out.println("m start ");
        //System.out.println(Thread.currentThread().getName());
        while (runing == 1) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("m runing..." + Thread.currentThread().getName());
        }
        System.out.println("m end ...");
    }

    public static void main(String[] args) {
        VolatileDemo v = new VolatileDemo();
        new Thread(v::m, "t1").start();
        new Thread(() -> {
            v.m();
        }, "t2").start();
        try {
            //Thread.sleep(1000);
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        v.runing = 2;
    }
}
